package cn.cms.dao;

import cn.cms.model.SysRole;
import org.jfaster.mango.annotation.DB;
import org.jfaster.mango.annotation.Rename;
import org.jfaster.mango.annotation.ReturnGeneratedId;
import org.jfaster.mango.annotation.SQL;

import java.util.List;

@DB(table = "sys_role")
public interface SysRoleDao {

    final static String COLUMNS = " id, pid, role_name, menu_ids, level, sort, update_time, create_time, status, note ";

    final static String ICOLUMNS = " pid, role_name, menu_ids, level, sort, update_time, create_time, status, note ";

    @SQL("insert into #table (" + ICOLUMNS +
            " )   values (:t.pid, :t.roleName, :t.menuIds, " +
            "      :t.level, :t.sort, :t.updateTime, :t.createTime, " +
            "      :t.status, :t.note " +
            "      )"
    )
    @ReturnGeneratedId
    public int save(@Rename("t") SysRole record);

    @SQL("select IFNULL(max(sort),1) from #table where pid=:1 ")
    public int getMaxSort(int pid);

    @SQL("select level from #table where id=:1 ")
    public int getLevelById(int id);

    @SQL(
            "update #table" +
                    "    set " +
                    " role_name = :t.roleName, " +
                    " menu_ids = :t.menuIds, " +
                    "      level = :t.level, " +
                    "      sort = :t.sort, " +
                    "      update_time = :t.updateTime, " +
                    "      status = :t.status, " +
                    "      note = :t.note " +
                    " where id=:t.id "
    )
    public void update(@Rename("t") SysRole record);

    @SQL("delete from #table where id=:1")
    public void delete(int id);

    @SQL("delete from #table where id=:id")
    public void delete(SysRole record);

    @SQL("select " + COLUMNS + " from #table where id=:1")
    public SysRole get(int id);

    @SQL("select " + COLUMNS + " from #table where id in (:1)")
    public List<SysRole> get(List<Integer> roleIds);

    @SQL("select  max(level) from #table where id in (:1)")
    public int getMaxLevel(List<Integer> roleIds);

    @SQL("select " + COLUMNS + " from #table where id=:id")
    public SysRole get(SysRole record);

    @SQL("select " + COLUMNS + " from #table where 1=1 and pid = -1 " +
            " order by sort asc "
    )
    public List<SysRole> getTop();

    @SQL("select " + COLUMNS + " from #table where pid = :1 order by sort asc ")
    public List<SysRole> getByPid(int pid);

}
